// 正式
let baseUrl = 'https://nearby.lefujin.cn/api/';
// 测试
// let baseUrl = 'https://nearby-v2.lefujin.cn/api/';
// let baseUrl = 'https://quli-dev.lefujin.cn/api/';


let $prefix = 'http://qiniu-cdn.maeiyun.com//h5static/';



// 封装的uni.request请求
function $get(url, data = {}, showToast = true, callback, header = "application/json") {
	// uni.setStorageSync('token', '0662395d6d8c4f6e30655f90f651da0b');
	// uni.setStorageSync('cid', 'af2a3167dbf0fc06');
	// uni.setStorageSync('terminal', 'ALN-AL80');
	const token = uni.getStorageSync('token') || '';
	const cid = uni.getStorageSync('cid') || '';
	const terminal = uni.getStorageSync('terminal') || '';
	return new Promise(function(resolve, reject) {
		// url = url.includes('http') ? url : baseUrl + url
		uni.request({
			url: baseUrl + url,
			method: 'GET',
			data: data,
			dataType: 'json',
			header: {
				"token": token,
				"cid": cid,
				"terminal": terminal,
				// 'Content-Type': header,
				"Control-Allow-Origin": true,
				"platform": "H5",
				"version": "3.4.1"
			},
			success: function(res) {
				if (res.statusCode == 200) {
					if (res.data.code == 0 || res.data.code === 1) {
						resolve(res.data)
					} else {
						if (callback) {
							callback(res.data)
						}
						if (showToast) {
							uni.showToast({
								title: res.data.msg || '',
								icon: "none"
							})
						}

					}
				} else {
					console.log(res);
				}
			},
			fail: function(e) {
				console.log(e.errMsg);
				console.log(e);
				console.log('fail');
				uni.showToast({
					title: '网络不给力',
					icon: "none"
				})
				reject(e)
			}
		})
	})
}

function $post(url, data = {}, showToast = true, callback, header = "application/json") {
	// uni.setStorageSync('token', '0662395d6d8c4f6e30655f90f651da0b');
	// uni.setStorageSync('cid', 'af2a3167dbf0fc06');
	// uni.setStorageSync('terminal', 'ALN-AL80');
	// console.log(uni.getStorageSync('token'),"<===============")
	const token = uni.getStorageSync('token');
	const cid = uni.getStorageSync('cid') || '';
	const terminal = uni.getStorageSync('terminal') || '';
	return new Promise(function(resolve, reject) {
		uni.request({
			url: baseUrl + url,
			method: 'POST',
			data: data,
			dataType: 'json',
			header: {
				"token": token,
				"cid": cid,
				"terminal": terminal,
				'Content-Type': 'application/x-www-form-urlencoded',
				"platform": "H5",
				"version": "3.4.1"
			},
			// header:header,
			success: function(res) {
				if (res.statusCode == 200) {
					if (res.data.code === 0 || res.data.code === 1) {
						resolve(res.data)
					} else {
						if (callback) {
							callback(res.data)
						}
						if (showToast) {
							uni.showToast({
								title: res.data.msg || '',
								icon: "none"
							})
						}
					}
				} else {
					console.log(res);
				}

			},
			fail: function(e) {
				console.log(e.errMsg);
				console.log(e);
				console.log('fail');
				uni.showToast({
					title: '网络不给力',
					icon: "none"
				})
				reject(e)
			}
		})
	})
}

// 普通鉴黄上传
function $upload(url, data = {}, header = "application/json") {
	// console.log(uni.getStorageSync('token'),"<===============")
	loading();
	const token = uni.getStorageSync('token')
	const cid = uni.getStorageSync('cid') || '';
	const terminal = uni.getStorageSync('terminal') || '';
	return new Promise(function(resolve, reject) {
		uni.uploadFile({
			url: baseUrl + '/upload/permanent',
			filePath: url,
			fileType: 'image',
			formData: data,
			success: (res) => {
				uni.hideLoading();
				var code = JSON.parse(res.data).code;
				if (code == 0) {
					var data = JSON.parse(res.data).data;
					if (data.status == 1) {
						uni.showToast({
							icon: "none",
							title: JSON.parse(res.data).msg,
							duration: 3000
						});
					}
					resolve(data);
				} else {
					uni.showToast({
						icon: "none",
						title: JSON.parse(res.data).msg,
						duration: 3000
					});
				}
			},
			fail(err) {
				console.log(err);
				uni.showToast({
					icon: "none",
					title: err,
					duration: 6000
				});
			}
		})
	})
}

function loading() {
	uni.showLoading({
		title: '上传中...',
	})
}

// OCR 上传
function $uploadOCR(url, data = {}, header = "application/json") {
	// console.log(uni.getStorageSync('token'),"<===============")
	const token = uni.getStorageSync('token')
	const cid = uni.getStorageSync('cid') || '';
	const terminal = uni.getStorageSync('terminal') || '';
	return new Promise(function(resolve, reject) {
		uni.uploadFile({
			url: baseUrl + '/upload/uploadOcr',
			filePath: url,
			fileType: 'image',
			formData: data,
			success: (res) => {
				resolve(JSON.parse(res.data));
			}
		})
	})
}

// 封装的请求方法需要暴露出去
module.exports = {
	$get,
	$post,
	$upload,
	$uploadOCR,
}